/**
 * calculator
 *
 * @author : tzy
 * @Date : 2021-03-16
 * @Description:
 **/

package com.tang.calculator.symbols;

import com.tang.calculator.context.CalculateContext;
import com.tang.calculator.utils.CheckUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Stack;

@Component
public class CommaSymbol implements OperatorSymbol, Calculation {
    @Autowired
    CheckUtils checkUtils;

    @Override
    public int getPriority() {
        return 1;
    }

    @Override
    public void operate(CalculateContext context) {
        Stack<OperatorSymbol> operatorStack = context.getOperatorStack();
        if (operatorStack.peek() instanceof CommaSymbol) {
            operatorStack.push(this);
        } else {
            while (!operatorStack.isEmpty() && operatorStack.peek().getPriority() > this.getPriority()) {
                OperatorSymbol operatorSymbol = operatorStack.pop();
                operatorSymbol.simpleOperate(context);
            }
            operatorStack.push(this);
        }
    }

    @Override
    public void simpleOperate(CalculateContext context) {
        Stack<ValueSymbol> valueStack = context.getValueStack();
        context.getTempValues().add(valueStack.pop());
    }

    @Override
    public String getName() {
        return ",";
    }

    /**
     * 校验
     *
     * @param context
     * @return
     */
    @Override
    public String check(CalculateContext context,String str) {
        if (context.getFormula().contains(getName())) {
            return checkUtils.coordinate(context,str,getName());
        }
        return null;
    }
}
